package com.qf.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;


/**
 * @Description: 公用常量
 * @Data: 2022/8/30 22:57
 * @Pacakge: com.qf.config
 * @ClassName: JdbcConfig
 * @Author: fan
 */
@Configuration
@ComponentScan(basePackages = "com.qf")
@PropertySource(value="classpath:jdbc.properties")
public class JdbcConfig {
    @Value("${driverClass}")
    private String driverClass;
    @Value("${url}")
    private String url;
    @Value("${user}")
    private String user;
    @Value("${password}")
    private String password;
    @Bean(name = "queryRunner")
    public QueryRunner getQueryRunner(){
        return new QueryRunner(getDataSource());
    }
    @Bean
    public DataSource getDataSource(){
        DataSource dataSource = new DruidDataSource();
        ((DruidDataSource)dataSource).setDriverClassName(driverClass);
        ((DruidDataSource)dataSource).setUrl(url);
        ((DruidDataSource)dataSource).setUsername(user);
        ((DruidDataSource)dataSource).setPassword(password);
        return dataSource;
    }
}
